package cn.mill.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.mill.common.Context;
import cn.mill.common.Protocol;
import cn.mill.common.annotation.SPI;
import cn.mill.common.message.Message;
import cn.mill.core.utils.NormUtil;

@SPI("file")
public class DefaultProtocol implements Protocol<String, InputStream> {
	private static Logger logger = LoggerFactory.getLogger(DefaultProtocol.class);
	
	@Override
	public Message<InputStream> invoker(Context invoke, Message<String> inputMessage) {
		DefaultMessage<InputStream> message = new DefaultMessage<InputStream>();
		try {
			String uri = (String) invoke.getMapping();
			uri = NormUtil.getUri(invoke.getNode(), uri);
			logger.debug("=============>" + uri);
			message.setDataBlock(new FileInputStream(new File(new URI(uri))));
		} catch (IOException | URISyntaxException e) {
			logger.error("", e);
		}
		return message;
	}
	
}
